Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Java Datatypes

Int in Java

Int datatype in Java

What is Int datatype?

The int data type is a signed 32-bit integer type. This means that it can store values from -2,147,483,648 to 2,147,483,647. Int variables are declared by using the int keyword.For example,
Int declaration
int i,j; int a=10; int b=-20; int c=a+b; int d=a*a-b; int e=214483648; int f=234.0244 (compilation error)
Java int basic example
public class Main{ public static void main(String[] args){ int age = 21; int mark = 175; int score = 75; System.out.println("The age is " + age); System.out.println("The mark is " + mark); System.out.println("The score is " + score); age += 1; mark += 5; score -= 10; System.out.println("The age is now " + age); System.out.println("The mark is now " + mark); System.out.println("The score is now " + score); } }
Let see the output

Output

The age is 21 The mark is 175 The score is 75 The age is now 22 The mark is now 180 The score is now 65
In this example, we declare three int variables: age, mark, and score. We then initialize the age variable to the value 21, the mark variable to the value 175, and the score variable to the value 75. We then print the values of the variables to the console. Next, we add 1 to the value of the age variable, add 5 to the value of the mark variable, and subtract 10 from the value of the score variable. We then print the values of the variables to the console again. Here are some key points to understand about the int datatype: Range: The int datatype has a moderate range of values compared to other numeric datatypes like short and long. It can hold values from -2.1 billion to 2.1 billion. Memory: Since it uses 32 bits (4 bytes) of memory, an int variable occupies more memory compared to byte and short but less memory compared to long. Operations: You can perform a wide range of arithmetic and logical operations on int values, making it suitable for various mathematical calculations. Type Casting: When performing operations involving int and other numeric types (such as long or float), you might need to explicitly cast the values to avoid potential data loss. Default for Whole Numbers: In many cases, int is the default choice for storing whole numbers in Java. When you declare a numeric literal without a decimal point, Java assumes it's an int. Use Cases: The int datatype is commonly used for counting, indexing, calculations, and other scenarios that involve whole numbers. Performance: On modern computer architectures, working with int values is usually efficient due to hardware-level optimizations for 32-bit operations.

  📌TAGS

★Int in Java ★Int datatype ★Java Int ★datatype ★Integer ★Java integer ★Java int size

Tutorials